<?php

namespace App\Models\Train\TrainHistory\Repositories;

use App\Models\Common\Area\Repositories\Interfaces\AreaRepositoryInterface;
use App\Models\Train\Stations\Repositories\Interfaces\StationRepositoryInterface;
use App\Models\Train\TrainHistory\TrainHistory;
use App\Trip\Repository\BaseRepository;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\Collection;

class TrainHistoryRepository extends BaseRepository implements Interfaces\TrainHistoryRepositoryInterface
{
    public function __construct(TrainHistory $model)
    {
        parent::__construct($model);
    }

    /**
     * @throws Exception
     */
    public function addByUserId(string $user_id, array $data): TrainHistory
    {
        $recode = $this->findOneByUserCode($user_id, $data['from_code'], $data['to_code']);
        if ($recode) {
            // 更新时间~~
            $recode->update([
                'updated_at' => ($data['updated_at'] ?? '') ?: Carbon::now()->toDateTimeString(),
            ]);
            return $recode;
        }

        $from_name    = "";
        $from_name_uy = "";
        $to_name      = "";
        $to_name_uy   = "";
        $areaRepo     = app(AreaRepositoryInterface::class);
        $stationRepo  = app(StationRepositoryInterface::class);
        switch ($data['from_type']) {
            case 0:// 城市
            case 1:// 区域
                $from         = $areaRepo->findByCode($data['from_code']);
                $from_name    = $from->getAreaName() ?? '';
                $from_name_uy = $from->getAreaNameUy() ?? '';
                break;
            case 2: // 站点
                $from         = $stationRepo->findOneByCode($data['from_code']);
                $from_name    = $from->getStationName() ?? '';
                $from_name_uy = $from->getStationNameUy() ?? '';
                break;
        }


        switch ($data['to_type']) {
            case 0:// 城市
            case 1:// 区域
                $to         = $areaRepo->findByCode($data['to_code']);
                $to_name    = $to->getAreaName() ?? '';
                $to_name_uy = $to->getAreaNameUy() ?? '';
                break;
            case 2:
                $to         = $stationRepo->findOneByCode($data['to_code']);
                $to_name    = $to->getStationName() ?? '';
                $to_name_uy = $to->getStationNameUy() ?? '';
                break;
        }


        return $this->create([
            'user_id'      => $user_id,
            'from_type'    => $data['from_type'],
            'to_type'      => $data['to_type'],
            'from_code'    => $data['from_code'],
            'to_code'      => $data['to_code'],
            'from_name'    => $from_name,
            'from_name_uy' => $from_name_uy,
            'to_name'      => $to_name,
            'to_name_uy'   => $to_name_uy,
        ]);
    }

    public function findOneByUserCode(string $user_id, string $from_code, string $to_code): ?TrainHistory
    {
        return $this->findOneBy([
            'user_id'   => $user_id,
            'from_code' => $from_code,
            'to_code'   => $to_code,
        ]);
    }

    public function listByUserId(string $user_id, int $limit = 6): Collection
    {
        return $this->model->where('user_id', $user_id)
                           ->orderByDesc('updated_at')
                           ->limit(6)
                           ->get();
    }

    public function clearByUserId(string $user_id): bool
    {
        return $this->model->where('user_id', $user_id)->delete();
    }
}